package other;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javafx.scene.layout.*;

// 异常处理（程序出现异常时按照你所希望的去处理）
public class ExceptionDemo extends Application {
	private Pane root = new Pane();
	private Button button;
	private TextField tf;
	
	public static void main(String[] args) {
		Application.launch(args);
	}
	
	@Override
	public void start(Stage a) {
		
		tf = new TextField();
		tf.setPromptText("请输入正确的年龄");
		tf.setFocusTraversable(false);
		tf.setLayoutX(100);  
		tf.setLayoutY(30); 
		
		button = new Button("click me");
		button.setLayoutX(30);
		button.setLayoutY(30);
		root.getChildren().addAll(tf, button);
		
		Scene scene = new Scene(root, 300, 200);
		a.setScene(scene);
		a.show();
		
		// 事件
		button.setOnMouseClicked(e -> showAge());
	}
	
	// 显示年龄
	private void showAge() {
		String ageString = tf.getText();
		try {
			int age = Integer.parseInt(ageString);
			System.out.println("age " + "is " + age);
			
		} catch(NumberFormatException e) {
			System.out.println("error: not an integer");
		}
	}
}





